<?php

/*
 * 
 */

include_once dirname(__FILE__)."/basedao.php";
include_once dirname(__FILE__)."/../objects/topictype.php";
class TopicTypesDAO extends BaseDAO{
	/**
	 * 增加一个帖子类型
	 * @param type $topic_type
	 */
	public function insertTopicType($topictype){
		$this->getConn()->query("LOCK TABLES topictypes WRITE");
		$sqlstr = "INSERT INTO topictypes(topictype_name, topictype_description) VALUES(?,?)";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("ss",
				$topictype->getTopicTypeName(),
				$topictype->getTopicTypeDescription());
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}
	/**
	 * 删除帖子类型
	 * @param type $topic_type_id
	 */
	public function deleteTopicType($topictype_id){
		$this->getConn()->query("LOCK TABLES topictypes WRITE");
		$sqlstr = "DELETE FROM topictypes WHERE topictype_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i",
				$topictype_id);
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}
	/**
	 * 取得所有帖子分类
	 */
	public function getAllTopicTypes(){
		$topictypes = array();
		$this->getConn()->query("LOCK TABLES topictypes READ");
		$sqlstr = "SELECT topictype_id, topictype_name, topictype_description FROM topictypes";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->execute();
		$stmt->bind_result($topictype_id, $topictype_name, $topictype_description);
		while($stmt->fetch()){
			$topictype = new TopicType();
			$topictype->setTopicTypeId($topictype_id);
			$topictype->setTopicTypeName($topictype_name);
			$topictype->setTopicTypeDescription($topictype_description);
			array_push($topictypes, $topictype);
		}
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $topictypes;		
	}
}
?>
